Use Azure service principals with Azure PowerShell 您所在的位置:网站首页 powershell -eq Use Azure service principals with Azure PowerShell

Use Azure service principals with Azure PowerShell

2023-05-08 03:40| 来源: 网络整理| 查看: 265

Create an Azure service principal with Azure PowerShell Article 05/01/2023

Automated tools that use Azure services should always have restricted permissions. Instead of having applications sign in as a fully privileged user, Azure offers service principals.

An Azure service principal is an identity created for use with applications, hosted services, and automated tools to access Azure resources. This access is restricted by the roles assigned to the service principal, giving you control over which resources can be accessed and at which level. For security reasons, it's always recommended to use service principals with automated tools rather than allowing them to log in with a user identity.

This article shows you the steps for creating, getting information about, and resetting a service principal with Azure PowerShell.

Warning

When you create a service principal using the New-AzADServicePrincipal command, the output includes credentials that you must protect. As an alternative, consider using managed identities to avoid the need to use credentials.

Prerequisites If you choose to use Azure PowerShell locally: Install the Az PowerShell module. Connect to your Azure account using the Connect-AzAccount cmdlet. If you choose to use Azure Cloud Shell: See Overview of Azure Cloud Shell for more information. Create a service principal

Create a service principal with the New-AzADServicePrincipal cmdlet. When creating a service principal, you choose the type of sign-in authentication it uses.

Important

Beginning with Az PowerShell module version 7.x, New-AzADServicePrincipal no longer assigns the Contributor role to the service principal by default. To assign a specific role to a service principal, see Steps to add a role assignment.

Note

If your account doesn't have permission to create a service principal, New-AzADServicePrincipal returns an error message containing "Insufficient privileges to complete the operation". Contact your Azure Active Directory admin to create a service principal.

There are two types of authentication available for service principals: Password-based authentication, and certificate-based authentication.

Password-based authentication

Important

The default role for a password-based authentication service principal is Contributor. This role has full permissions to read and write to an Azure account. For information on managing role assignments, see Manage service principal roles.

Without any other authentication parameters, password-based authentication is used and a random password created for you. If you want password-based authentication, this method is recommended.

$sp = New-AzADServicePrincipal -DisplayName ServicePrincipalName

The returned object contains the PasswordCredentials.SecretText property containing the generated password. Make sure that you store this value somewhere secure to authenticate with the service principal. Its value won't be displayed in the console output. If you lose the password, reset the service principal credentials.

The following code allows you to export the secret:

$sp.PasswordCredentials.SecretText

The object returned from New-AzADServicePrincipal contains the Id and DisplayName members, either of which can be used for sign in with the service principal.

Important

Signing in with a service principal requires the tenant ID which the service principal was created under. To get the active tenant when the service principal was created, run the following command immediately after service principal creation:

(Get-AzContext).Tenant.Id Certificate-based authentication

Important

There is no default role assigned when creating a certificate-based authentication service principal. For information on managing role assignments, see Manage service principal roles.

Service principals using certificate-based authentication are created with the CertValue parameter. This parameter takes a base64-encoded ASCII string of the public certificate. This is represented by a PEM file, or a text-encoded CRT or CER. Binary encodings of the public certificate aren't supported. These instructions assume that you already have a certificate available.

$cert = $sp = New-AzADServicePrincipal -DisplayName ServicePrincipalName -CertValue $cert

The object returned from New-AzADServicePrincipal contains the Id and DisplayName properties, either of which can be used for sign in with the service principal. Clients which sign in with the service principal also need access to the certificate's private key.

Important

Signing in with a service principal requires the tenant ID which the service principal was created under. To get the active tenant when the service principal was created, run the following command immediately after service principal creation:

(Get-AzContext).Tenant.Id Get an existing service principal

A list of service principals for the active tenant can be retrieved with Get-AzADServicePrincipal. By default this command returns all service principals in a tenant. For large organizations, it may take a long time to return results. Instead, using one of the optional server-side filtering arguments is recommended:

DisplayNameBeginsWith requests service principals that have a prefix that match the provided value. The display name of a service principal is the value set with DisplayName during creation. DisplayName requests an exact match of a service principal name. Manage service principal roles

Azure PowerShell has the following cmdlets to manage role assignments:

Get-AzRoleAssignment New-AzRoleAssignment Remove-AzRoleAssignment

For more information on Role-Based Access Control (RBAC) and roles, see RBAC: Built-in roles.

The following example adds the Reader role and removes the Contributor role:

New-AzRoleAssignment -ApplicationId -RoleDefinitionName 'Reader' Remove-AzRoleAssignment -ObjectId -RoleDefinitionName 'Contributor'

Important

Role assignment cmdlets don't take the service principal object ID. They take the associated application ID, which is generated at creation time. To get the application ID for a service principal, use Get-AzADServicePrincipal.

Note

If your account doesn't have permission to assign a role, you see an error message that your account "doesn't have authorization to perform action 'Microsoft.Authorization/roleAssignments/write'". Contact your Azure Active Directory admin to manage roles.

Adding a role doesn't restrict previously assigned permissions. When restricting a service principal's permissions, the Contributor role should be removed.

The changes can be verified by listing the assigned roles:

Get-AzRoleAssignment -ServicePrincipalName ServicePrincipalName Sign in using a service principal

Test the new service principal's credentials and permissions by signing in. To sign in with a service principal, you need the applicationId value associated with it, and the tenant it's created under.

To sign in with a service principal using a password:

# Use the application ID as the username, and the secret as password $credentials = Get-Credential Connect-AzAccount -ServicePrincipal -Credential $credentials -Tenant

Certificate-based authentication requires that Azure PowerShell can retrieve information from a local certificate store based on a certificate thumbprint.

Connect-AzAccount -ServicePrincipal -Tenant -CertificateThumbprint -ApplicationId

For instructions on importing a certificate into a credential store accessible by PowerShell, see Sign in with Azure PowerShell

Reset credentials

If you forget the credentials for a service principal, use New-AzADSpCredential to add a new credential with a random password. This cmdlet doesn't support user-defined credentials when resetting the password.

Important

Before assigning any new credentials, you may want to remove existing credentials to prevent sign in with them. To do so, use the Remove-AzADSpCredential cmdlet:

Remove-AzADSpCredential -DisplayName ServicePrincipalName $newCredential = New-AzADSpCredential -ServicePrincipalName ServicePrincipalName Troubleshooting

If you receive the error: "New-AzADServicePrincipal: Another object with the same value for property identifierUris already exists.", verify that a service principal with the same name doesn't already exist.

Get-AzAdServicePrincipal -DisplayName ServicePrincipalName

If the existing service principal is no longer needed, you can remove it using the following example.

Remove-AzAdServicePrincipal -DisplayName ServicePrincipalName

This error can also occur when you've previously created a service principal for an Azure Active Directory application. If you remove the service principal, the application is still available. This application prevents you from creating another service principal with the same name.

You can use the following example to verify that an Azure Active Directory application with the same name doesn't exist:

Get-AzADApplication -DisplayName ServicePrincipalName

If an application with the same name does exist and is no longer needed, it can be removed using the following example.

Remove-AzADApplication -DisplayName ServicePrincipalName

Otherwise, choose an alternate name for the new service principal that you're attempting to create.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有